package test.testList;

import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

public class ReadFile {

    public static List<String> getFiles(String path) throws Exception{
        List<String> files = new ArrayList<String>();
        File file = new File(path);
        File[] tempList = file.listFiles();
        String separator = File.separator;
        /* "\\trust-tda-biz-server\\.git\" */
        String gitPath = separator +"trust-tda-biz-server" + separator +".git";

        if(tempList != null && tempList.length > 0) {
            for (int i = 0; i < tempList.length; i++) {
                File file1 = tempList[i];
                String filePath = file1.getPath();
                //System.out.println(filePath);
                //文件
                if (tempList[i].isFile()) {
                    String fileName = file1.getName();
                    /*
                    busin_pty   bpty      BUSIN_PTY   BPTY    BusinPty   Bpty    businPty   bpty
                    busin_unit  bunit     BUSIN_UNIT  BUNIT   BusinUnit  Bunit   businUnit  bunit
                    busin_role  brole     BUSIN_ROLE  BROLE   BusinRole  Brole   businRole  brole
                    busin_prtc  bprtc     BUSIN_PRTC  BPRTC   BusinPrtc  Bprtc   businPrtc  bprtc
                    busin_dept  bdept     BUSIN_DEPT  BDEPT   BusinDept  Bdept   businDept  bdept
                    busin_sys   bsys      BUSIN_SYS   BSYS    BusinSys   Bsys    businSys   bsys
                    busin_market  bmarket   BUSIN_MARKET BMARKET   BusinMarket Bmarket   businMarket  bmarket
                     */

                    if(fileName.contains("busin_market")){
                        //修改名称
                        renameFile(fileName,filePath,file1,"busin_market","bmarket");

                    }else if(fileName.contains("BUSIN_MARKET")){
                        //修改名称
                        renameFile(fileName,filePath,file1,"BUSIN_MARKET","BMARKET");

                    }else if (fileName.contains("BusinMarket")){
                        //修改名称
                        renameFile(fileName,filePath,file1,"BusinMarket","Bmarket");

                    }else if (fileName.contains("businMarket")){
                        //修改名称
                        renameFile(fileName,filePath,file1,"businMarket","bmarket");
                    }
                }
                //文件夹
                if (tempList[i].isDirectory() && !filePath.contains(gitPath)) {

                    //递归调用
                    getFiles(filePath);
                }
            }
        }

        return files;
    }

    private static void renameFile(String fileName,String filePath,File file,String target,String replacement){
        String newFileName = fileName.trim().replace(target,replacement);
        //System.out.println(newFileName);
        String newPath = filePath.substring(0,filePath.lastIndexOf("\\")) + "\\"+newFileName;
        //System.out.println(newPath);
        file.renameTo(new File(newPath));
    }
}
